﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Announcement.Model;
using Announcement.DAL.DataSetApplyTableAdapters;

namespace Announcement
{
    public partial class Repair : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApplyTable at = (ApplyTable)Session["ApplyTable"];
                string cCode = at.ContractCode;
                string applyer = at.Applicant;
                string houseLocal = at.HouseLocated;
                string houseNum = at.HouseNum;
                DateTime applyDate = (DateTime)at.ApplyDate;

                labCCode.Text = cCode;
                labName.Text = applyer;
                labLocal.Text = houseLocal;
                labNum.Text = houseNum;
                labApplyDate.Text = applyDate.ToString("yyyy年MM月dd日");

                if (Request.QueryString["isPrn"] == "true")
                {
                    btnSave.Visible = false;
                    btnPrn.Enabled = true;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            btnPrn.Enabled = true;
            btnSave.Enabled = false;

            //保存到数据库
            ApplyTable at = (ApplyTable)Session["ApplyTable"];
            var applyRow = new T_ApplyTableTableAdapter();

            //如果重复保存则返回
            if (applyRow.GetDataByContractCode(at.ContractCode).Rows.Count > 0)
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "已存在", @"alert('" + "该公告已存在,无需重复保存" + "');", true);
                return;
            }

            applyRow.Insert(at.ContractCode, at.Applicant, at.Legal, at.CardTpye,
                at.CardNum, at.Agents, at.Phone, at.CardType2, at.CardNum2,
                at.HouseLocated, at.HouseNum, at.ApplyType, at.ApplyDate, at.IsPay,at.Money);
            
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["AdminUserName"] = null;
            Session["UserName"] = null;
            Response.Redirect("~/Login.aspx");
        }
    }
}
